<?php
class Component extends Zend_Db_Table
{
	protected $_name = 'component';
	public function getProjectComponents($projectId)
	{
		$select = $this->_db->select()
							->from($this->_name)
							->where('project = ?', $projectId);
		//echo $select;
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	public function getList($pid)
	{
		$select = $this->_db->select()
							->from($this->_name,array('key'=>'id','value'=>'cname'))
							->where('project = ?',$pid);
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	public function getData($pid)
	{
		$select = $this->_db->select()
							->from(array('cp'=>$this->_name))
							/* ->join(array('comp_issues'=>'component_issues'),
								'cp.id = comp_issues.component',"count('comp_issues.id')  as issues")
							->group('cp.id')*/
							->join(array('nodeAssoc'=>'nodeassociation'),
							 					'cp.id = nodeAssoc.sink_node_id', "count(*)  as issues")
							->join(array('issue'=>'issue'),'issue.id = nodeAssoc.source_node_id',null)
							->group('cp.id')
							->where('cp.project = ?',$pid)
							->where('nodeAssoc.association_type = "IssueComponent"');
		$components = $this->getAdapter()->fetchAll($select);
		return $components;
	}
	
	public function getIssueComponents($id)
	{
		$select = $this->_db->select()
							->from(array('cp'=>$this->_name),array('id','cname'))
							/* ->join(array('comp_issues'=>'component_issues'),
											'cp.id=comp_issues.component',null)*/
							 ->join(array('nodeAssoc'=>'nodeassociation'),
							 					'cp.id = nodeAssoc.sink_node_id', null)
							->where('nodeAssoc.source_node_id = ?', $id)
							->where('association_type = "IssueComponent"');
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	public function getComponentIssues($cid, $projectId)
	{
		$select = $this->_db->select()
							->from(array('issue'=>'issue'),
													array('id','pkey','summary'))
							->joinLeft(array('priority'=>'priority'),'
												issue.priority=priority.id',
														array('priorityName'=>'pname',
															  'priorityIcon'=>'icon_url'))
							->joinLeft(array('issuestatus'=>'issuestatus'),
											'issuestatus.id = issue.issuestatus',
														array('issueStatusName'=>'pname',
															  'issueStatusIcon'=>'icon_url'))
							->joinLeft(array('resolution'=>'resolution'),
											'issue.resolution=resolution.id',
														array('resolutionName'=>'pname'))
							->joinLeft(array('issuetype'=>'issuetype'),
											'issue.issuetype=issuetype.id',
														array('issueTypeName'=>'pname',
															  'issueTypeIcon'=>'icon_url'))
							->join(array('nodeAssoc'=>'nodeAssociation'),'issue.id = nodeAssoc.source_node_id',null)
							->join(array('comp'=>$this->_name),'comp.id = nodeAssoc.sink_node_id',null)
							->group('issue.id')
							->where('nodeAssoc.association_type = "IssueComponent"')
							->where("comp.id = ?",$cid)
							->where("issue.project = ?", $projectId);
		$results = $this->getAdapter()->fetchAll($select);
		return $results;
	}
	
	public function getVersionData($pid, $vid)
	{
		$select = $this->_db->select()
							->from(array('cp'=>$this->_name),array('cname'))
							->join(array('nodeAssoc'=>'nodeassociation'),
											'cp.id = nodeAssoc.sink_node_id', null)
							->join(array('issue'=>'issue'),
											'issue.id = nodeAssoc.source_node_id',
												"count(*) as c_issues")
							->group('cp.id')
							->where("nodeAssoc.association_type = 'IssueComponent' AND (nodeAssoc.source_node_id IN (select nodeassociation.source_node_id from nodeassociation where nodeassociation.sink_node_id = $vid AND nodeassociation.association_type = 'IssueFixVersion'))")
							->where('cp.project = ?',$pid);
		//echo $select;					
		$versions = $this->getAdapter()->fetchAll($select);
		return $versions;
	}
}